package com.dao.impl;

import com.dao.MemberDao;
import com.dao.RedisGeneratorDao;
import com.entity.Condition;
import com.entity.Member;
import com.entity.MemberExample;
import com.mapper.MemberMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Repository
public class MemberDaoImpl extends RedisGeneratorDao implements MemberDao {
    @Resource
    MemberMapper memberMapper;
     public List<Member> getAll(){
         return memberMapper.getAll();
     }

    /**
     * 普通分页查询
     * @param pageInfo
     * @return
     */
    public List<Member> getMembersByPage(PageInfo pageInfo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)memberMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
         return memberMapper.getMembersByPage(map);
    }

    /**
     * 搜索分页查询
     * @param pageInfo
     * @return
     */
    public List<Member> searchMembersByPage(PageInfo pageInfo, List<Condition> conditions) throws ParseException {
        Map<String,Object> map = new HashMap<String,Object>();
        int  currPage =  pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        int offset  = (currPage - 1) * pageSize;

        MemberExample memberExample = new MemberExample();
        memberExample.setOffset(offset);
        memberExample.setLimit(pageSize);

        MemberExample.Criteria criteria = memberExample.createCriteria();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //设置分页
        for(Condition condition:conditions){
            String fdName = condition.getFdName();
            String value = condition.getValue();
            String value2 = condition.getValue2();
            switch(fdName){
                case "mname":
                    criteria.andMnameLike(value);
                    break;
                case "msex":
                    criteria.andMsexEqualTo(value);
                    break;
                case "mbirthday":
                    String op = condition.getOp();
                    if(op.equals("1")){criteria.andMbirthdayGreaterThanOrEqualTo(df.parse(value));}
                    else if(op.equals("2")){criteria.andMbirthdayLessThanOrEqualTo(df.parse(value));}
                    else if(op.equals("3")){criteria.andMbirthdayBetween(df.parse(value),df.parse(value2));}
                    break;
                default: break;
            }
        }

//搜索的统计也需要用exmple 提供的sql 语句来统计
        int itemCount = (int)memberMapper.countByExample(memberExample);
        pageInfo.setItemCount(itemCount);
        return memberMapper.selectByExample(memberExample);
    }

    /**
     * 查询基本信息带上全部关联对象
     * @param mno
     * @return
     */
    public Member memberByMnoWithAll(String mno){
        return memberMapper.memberByMnoWithAll(mno);//是使用selective 有值才插入
    }

    /**
     *
     * @param mno
     * @return
     */
    public Member getMemberByMno(String mno) throws Exception{
        return memberMapper.selectByPrimaryKey(mno);
    }

    /**
     *
     * @param member
     */
    public Integer add(Member member)throws Exception{
        return memberMapper.insertSelective(member);//是使用selective 有值才插入
    }
    /**
     *  根据主键修改信息
     * @param member
     */
    public Integer update(Member member)throws Exception{
        int result = memberMapper.updateByPrimaryKeySelective(member);
        //修改 需要对应的删除缓存先模糊查询出key
        Set<Serializable> keys =  redisTemplate.keys("MembersPage*");

        redisTemplate.delete(keys);
        return result;//是使用selective 有值才插入
    }

    /**
     *
     * @param mno
     * @return
     */
    public Member getAccountByAccountName(String mno) throws Exception{
        return memberMapper.getAccountByAccountName(mno);
    }


}
